package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class EarthGrid extends JPanel {
  private static final long serialVersionUID = -1108120968981962997L;
  private static final int DEFAULT_GRID_SIZE = 15;
  
  private BufferedImage imgTransparent;
  private BufferedImage imgStatic;
  private float[] scales = { 1f, 1f, 1f, Constants.OPACITY }; //last index controls the transparency
  float[] offsets = new float[4];
  private int pixelsPerCellX; //number of pixels per latitudal division
  private int pixelsPerCellY; //number of pixels per longitudal division
  private int imgWidth; // in pixels
  private int imgHeight; // in pixels
  
  public EarthGrid() {
    try {
      URL imgURL = getClass().getResource("earth-white.jpg");
      imgStatic = ImageIO.read(imgURL);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    
    setGridSize(DEFAULT_GRID_SIZE);
    setIgnoreRepaint(true);
  }
  
  public void setGridSize(int degreeSeparation) {
    int numCells = 360 / degreeSeparation;      
    pixelsPerCellX = imgStatic.getWidth() / numCells;
    imgWidth = numCells * pixelsPerCellX;

    pixelsPerCellY = imgStatic.getHeight() / numCells;
    imgHeight = numCells * pixelsPerCellY;

    //create an image capable of transparency; then draw our image into it
    imgTransparent = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics g = imgTransparent.getGraphics();
    g.drawImage(imgStatic, 0, 0, imgWidth, imgHeight, null);    
  }
  
  public void paint(Graphics g) {
    //the order in which these are called does matter
    drawCellColors(g);
    drawTransparentImage(g);
    drawGrid(g);
  }
  
  private void drawCellColors(Graphics g) {
    g.setColor(Color.RED);
    for (int i = 0; i < imgWidth; i += pixelsPerCellX) {
      for (int j = 0; j < imgHeight; j += pixelsPerCellY) {
        g.fillRect(i, j, pixelsPerCellX, pixelsPerCellY);
      }
    }
  }
  
  private void drawTransparentImage(Graphics g) {    
    
    RescaleOp rop = new RescaleOp(scales, offsets, null);
    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(imgTransparent, rop, 0, 0);
  }
  
  private void drawGrid(Graphics g) {
    g.setColor(Color.white);
    
    for(int x = 0; x < imgWidth; x += pixelsPerCellX) {
      g.drawLine(x, 0, x, imgHeight);      
    }
    
    for(int y = 0; y < imgHeight; y += pixelsPerCellY) {
      g.drawLine(0, y, imgWidth, y);
    }
    
    g.setColor(Color.blue);
    g.drawLine(imgWidth/2, 0, imgWidth/2, imgHeight); //prime meridian
    g.drawLine(0, imgHeight/2, imgWidth, imgHeight/2); // equator
  }
  
  public int getWidth() {
    return imgWidth;
  }
  
  public void setMapOpacity(float value) {
    scales[3] = value;
//    paint(getGraphics());
//    repaint();
  }

}
